package servlet.order;

import util.DbUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/BuyGoodsServlet")
public class BuyGoodsServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
//        获取登录时的user_id
//        int user_id = 896733;
        int user_id = (int)session.getAttribute("zhi");

        String goods_pic = session.getAttribute("zhaopian").toString();
        int goods_price = Integer.parseInt(request.getParameter("price"));
        int num = Integer.parseInt(request.getParameter("num"));
        String address = request.getParameter("address");
        String address_name = request.getParameter("address_name");
        String phone = request.getParameter("phone");
        DbUtil db = new DbUtil();
        String sql = "insert into  orders(user_id,goods_pic,goods_price,num,address,address_name,phone) values("+user_id+","+goods_pic
        +","+goods_price+","+num+",'"+address+"','"+address_name+"','"+phone+"')";
        System.out.println(sql);
        if (db.executeUpdate(sql)>0){
            response.sendRedirect("FindOrdersServlet");
        }else {
            response.sendRedirect("FindOrdersServlet");
        }
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
